package microBlogger.screens;


import context.*;


import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import microBlogger.AccountRecordStore;
import microBlogger.MicroBlogger;

// ******************************************************************
// *** CLASS: AccountScreen
// ******************************************************************
public class AccountScreen extends Form implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command okCommand=new Command("OK",Command.OK,0);
  private static final Command cancelCommand=new Command("Cancel",Command.CANCEL,0);

//  private MicroGoogleTalk midlet;
  private Display display;
  private Displayable prev;
  private TextField nickName;
  private TextField userID;
  private TextField password;

  public AccountScreen(final MicroBlogger midlet,final AccountRecordStore account)
  { super("Account:");
//    this.midlet=midlet;
    this.display=Display.getDisplay(midlet);
    if(DEBUG) Debug.println("New AccountScreen");
    this.addCommand(okCommand);
    this.addCommand(cancelCommand);
    
    nickName=new TextField("nick name",account.getNickName(),AccountRecordStore.MAX_NICK,TextField.ANY);
    userID=new TextField("user ident",account.getUserID(),AccountRecordStore.MAX_USERID,TextField.ANY);
    password=new TextField("password",account.getPassword(),AccountRecordStore.MAX_PASSWORD,TextField.PASSWORD);
    append(nickName);
    append(userID);
    append(password);
    
    
    this.setCommandListener(new CommandListener()
    { public void commandAction(Command c,Displayable s)
      {	if(c==cancelCommand) display.setCurrent(prev);
    	if(c==okCommand)
    	{ // update
          String nck=nickName.getString();
          String uid=userID.getString();
          String pwd=password.getString();
          account.update(nck,uid,pwd);
    	  display.setCurrent(prev);
    	}
    	  
      }
    });
  }
  
  public void show()
  { prev=display.getCurrent();
    display.setCurrent(this);
  }
}
